package com.toobulkeh.search;
//package com.toobulkeh.search;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Random;

public class ImageSearchResult {

	ArrayList<Image> images;
	Random rand;
	int currIndex;
	
	public ImageSearchResult() {
		images = new ArrayList<Image>();
		rand = new Random(System.currentTimeMillis());
		currIndex = 0;
	}
	
	public void addImages(ArrayList<String> urls,ArrayList<String> thumbs) {
		for(int i = 0; i < urls.size(); i++)
			addImage(urls.get(i),thumbs.get(i));
	}
	
	public void addImages(ArrayList<Image> toAdd) {
		for(int i = 0; i < toAdd.size(); i++)
			addImage(toAdd.get(i));
	}
	
	public void addImage(String url, String thumb) {
		try {
			addImage(new Image(url,thumb));
		} catch (MalformedURLException e) {
			System.err.println("Bad URL for Image: "+url);
			System.err.println("Bad THUMB for Image: "+thumb);
		}
	}
	
	public void combine(ImageSearchResult toAdd) {
		addImages(toAdd.getImages());
	}
	
	public void addImage(Image x) {
		images.add(x);
	}
	
	public int getSize() {
		return images.size();
	}
	
	public Image getNextImage() {
		Image curr = images.get(currIndex);
		currIndex++;
		return curr;
	}
	
	public Image getImage(int index) {
		return images.get(index);
	}
	
	public ArrayList<Image> getImages() {
		return images;
	}
	
	public Image getRandImage() {
		int index = rand.nextInt(images.size());
		//System.out.println("Random index: "+index);
		return getImage(index);
	}
	
	public String toString() {
		String toRet = "";
		
		for(int i = 0; i < images.size(); i++)
			toRet+=images.get(i).getURL() + "\n";
		
		return toRet;
		
	}
	
}
